import { Global, Module } from "@nestjs/common";
import { ConfigModule, ConfigService } from "@nestjs/config";
import { ElasticsearchModule } from "@nestjs/elasticsearch";

@Global()
@Module({
    imports: [
        ElasticsearchModule.registerAsync({
            imports: [ConfigModule],
            useFactory: async (configService: ConfigService) => {
                const options: any = {
                    node: configService.get<string>("ELASTICSEARCH_NODE", "http://localhost:9200"),
                };
                const username = configService.get<string>("ELASTICSEARCH_USERNAME");
                const password = configService.get<string>("ELASTICSEARCH_PASSWORD");
                if (username && password) {
                    options.auth = { username, password };
                }
                return options;
            },
            inject: [ConfigService],
        }),
    ],
    exports: [ElasticsearchModule],
})
export class EsModule {}
